﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using GameServer.Properties;
using Backend.Packets;

namespace GameServer.Backend.Managers
{
    public class AccountManager
    {
        private readonly string m_sAccountsPath;

        public AccountManager()
        {
            m_sAccountsPath = Settings.Default.ConfigPath + Settings.Default.Accounts;
        }

        private bool Logout(GameClient Client)
        {
            Client.LoggedIn = false;

            return true;
        }

        /// <summary>
        /// Parses a login request.  Login strings are in the form of UserName,Password.
        /// </summary>
        /// <param name="Packet"></param>
        /// <param name="Client"></param>
        /// <returns></returns>
        private bool Login(AccountPacket Packet, GameClient Client)
        {
            XmlTextReader Reader = new XmlTextReader(m_sAccountsPath);
            
            string[] sData = Encoding.ASCII.GetString(Packet.Data).Split(',');

            //Doesn't contain a Login and Password.
            if (sData.Length != 2)
                return false;

            string sUserName = sData[0];
            string sPassword = sData[1];

            while (Reader.Read())
            {
                if (Reader.Name == "Account")
                {
                    if (Reader.GetAttribute("UserName") == sUserName && Reader.GetAttribute("Password") == sPassword)
                    {
                        Client.LoggedIn = true;
                        Client.UserName = Reader.GetAttribute("UserName");
                        
                        return true;
                    }
                }
            }

            return false;
        }

        private bool CreateAccount(AccountPacket Packet, GameClient Client)
        {
            XmlTextWriter Writer = new XmlTextWriter(m_sAccountsPath, Encoding.ASCII);

            return true;
        }

        public void ParsePacket(AccountPacket Packet, GameClient Client)
        {
            switch(Packet.PacketCommand)
            {
                case AccountPacket.AccountCommand.ACCOUNT_LOGIN_REQUEST:
                    {
                        AccountPacket Payload = new AccountPacket();
                        Payload.PacketCommand = AccountPacket.AccountCommand.ACCOUNT_LOGIN_RESPONSE;

                        if (Login(Packet, Client))
                        {
                            Payload.Response = "Hello " + Packet.Login;
                            Client.Send(Payload);
                        }
                        else
                        {
                            Payload.Response = "Login Failed.";
                            Client.Send(Payload);
                        }
                        break;
                    }
                case AccountPacket.AccountCommand.ACCOUNT_LOGOUT_REQUEST:
                    {
                        Logout(Client);
                        break;
                    }
                case AccountPacket.AccountCommand.ACCOUNT_NEW_ACCOUNT_REQUEST:
                    {
                        CreateAccount(Packet, Client);
                        break;
                    }
            }
        }
    }
}
